#include "networkloginrequest.h"
#include "networkfailedresponse.h"
#include "networkloginedresponse.h"
#include <json.h>

#include <QBuffer>
#include <QDebug>
#include <QUrl>
#include <QNetworkReply>
#include <QTextStream>

using namespace network;
LoginRequest::LoginRequest(const QString &user, const QString &password, QObject *parent) :
    HttpGetRequest(parent),
    m_user(user),
    m_password(password)
{
}

QUrl LoginRequest::url() const
{
    QString url = QString("http://dev.mapi2.me.zing.vn/frs/mapi2/session?method=user_dologinmobilemapping&username=%1&password=%2")
            .arg(m_user, m_password);
    qDebug() << url;
    return QUrl(url);
}

Response *LoginRequest::parse(QByteArray *data)
{
    JsonReader reader;
    QVariant variant;
    QTextStream stream(data);
    stream.setCodec("UTF-8");
    if (!reader.read(stream, variant))
            return new FailedResponse(FailedResponse::JsonInvalided,
                                      QString("Json syntax error"),
                                      this);
    if (variant.type() != QVariant::Map)
        return new FailedResponse(FailedResponse::PropertiesInvalided,
                                  QString("The reponse isn't a map"),
                                  this);
    QVariantMap map = variant.toMap();
    if (!map.contains("error_code"))
        return new FailedResponse(FailedResponse::PropertiesInvalided,
                                  QString("The \"error_code\" is missing"),
                                  this);

    QVariant errorCode = map.value("error_code");
    if (errorCode.type() != QVariant::Int) {
        QVariant errorMessage = map.value("error_message");
        return new FailedResponse(FailedResponse::PropertiesInvalided,
                                  errorMessage.toString(),
                                  this);
    }

    if (errorCode.toInt() != 0) {
        QVariant errorMessage = map.value("error_message");
        qDebug() << errorCode;
        return new FailedResponse(FailedResponse::PropertiesInvalided,
                                  errorMessage.toString(),
                                  this);
    }


    if (!map.contains("data"))
        return new FailedResponse(FailedResponse::PropertiesInvalided,
                                  QString("The \"data\" value is missing"),
                                  this);

    QVariant properties = map.value("data");
    if (properties.isNull())
        return new FailedResponse(FailedResponse::PropertiesInvalided,
                                  "The \"data\" value invalided",
                                  this);

    return parse(properties);
}

Response *LoginRequest::parse(const QVariant &properties)
{
    if (properties.type() != QVariant::Map)
        return new FailedResponse(FailedResponse::PropertiesInvalided,
                                  QString("The \"data\" value is invalided"),
                                  this);
    QVariantMap map = properties.toMap();
    if (!map.contains("uid"))
        return new FailedResponse(FailedResponse::PropertiesInvalided,
                                  QString("The \"uid\" value missing"),
                                  this);
    QVariant userId = map.value("uid");
    if (userId.type() != QVariant::Int)
        return new FailedResponse(FailedResponse::PropertiesInvalided,
                                  QString("The \"uid\" value is invalided"),
                                  this);

    if (!map.contains("urn"))
        return new FailedResponse(FailedResponse::PropertiesInvalided,
                                  QString("The \"urn\" value missing"),
                                  this);

    QVariant userName = map.value("urn");
    if (userName.isNull())
        return new FailedResponse(FailedResponse::PropertiesInvalided,
                                  QString("The \"urn\" value is invalided"),
                                  this);

    if (!map.contains("dpn"))
        return new FailedResponse(FailedResponse::PropertiesInvalided,
                                  QString("The \"dpn\" value missing"),
                                  this);

    QVariant displayName = map.value("dpn");
    if (displayName.isNull())
        return new FailedResponse(FailedResponse::PropertiesInvalided,
                                  QString("The \"dpn\" value is invalided"),
                                  this);

    if (!map.contains("skey"))
        return new FailedResponse(FailedResponse::PropertiesInvalided,
                                  QString("The \"skey\" value missing"),
                                  this);

    QVariant sessionKey = map.value("skey");
    if (sessionKey.isNull())
        return new FailedResponse(FailedResponse::PropertiesInvalided,
                                  QString("The \"skey\" value is invalided"),
                                  this);

    if (!map.contains("avatar"))
        return new FailedResponse(FailedResponse::PropertiesInvalided,
                                  QString("The \"skey\" value missing"),
                                  this);

    QVariant avatar = map.value("avatar");
    if (avatar.isNull())
        return new FailedResponse(FailedResponse::PropertiesInvalided, QString("The \"avatar\" value invalid"));
    return new LoginedResponse(userId.toInt(),
                               userName.toString(),
                               displayName.toString(),
                               avatar.toString(),
                               sessionKey.toString(),
                               this);
}
